package com.example.camerafacedetect.faceframe;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.hardware.Camera;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.google.mlkit.vision.face.Face;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BitmapConsumer implements Runnable {
    private ExecutorService pool = Executors.newSingleThreadExecutor();
    private Activity UIContext;
    private Camera camera;
    private FaceView faceView;
    public BitmapConsumer(Activity pContext, Camera pCamera, FaceView pFaceView) {
        this.UIContext = pContext;
        this.camera = pCamera;
        this.faceView = pFaceView;
    }

    public void start(){
        pool.submit(this);
    }

    @Override
    public void run() {
        // 只要没有接收到暂停的信号，就继续处理
        while(!GlobalVariable.pause_flag){
            Log.e("++++++++++", "Consumer continue run.....");
            try {
                Bitmap bitmap = GlobalVariable.bitmap_queue.poll();
                if(bitmap == null){
                    // 首先判断是否是暂停活动了
                    if(GlobalVariable.pause_flag){
                        break;
                    }
                    // 非暂停活动，才是挂起1秒钟再消费图片
                    else {
                        Thread.sleep(1000);
                        continue;
                    }
                }
                // 进行人脸检测
                FaceDetectorUtils.detectFace(bitmap, new FaceDetectorUtils.Callback() {
                    @Override
                    public void onFaceDetected(final Bitmap bm) {
                        Log.e("yubo", "face detected...");
                        bindBitmapToFaceView(bm);
                    }

                    @Override
                    public void onFaceNotDetected(Bitmap bm) {
                        bm.recycle();
                        if (faceView != null) {
                            faceView.clear();
                        }
                    }
                });

                if (camera != null) {
                    camera.stopPreview();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Log.e("+++++ Consumer", "Consumer get the pause flag, stop hanle");
        closeFaceView();
    }

    public void bindBitmapToFaceView(final Bitmap bm) {
        UIContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // 如果当前状态为暂停，就不需要要再绑定图像，可能会和覆盖掉黑屏设置
                if(GlobalVariable.pause_flag){
                    return;
                }
                if (faceView != null) {
                    float scaleRate = bm.getWidth() * 1.0f / getScreenSize().x;
                    faceView.setScaleRate(scaleRate);
                    faceView.setFaces(bm);
                    faceView.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    public void closeFaceView(){
        UIContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                faceView.black_screen();
            }
        });
    }

    /**
     * 获取手机屏幕的尺寸
     *
     * @return
     */
    private Point getScreenSize() {
        WindowManager manager = (WindowManager) UIContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        return new Point(outMetrics.widthPixels, outMetrics.heightPixels);
    }
}